// pages/EditText/index.js
const app = getApp();
const db = wx.cloud.database();
import {
	bynameUpdate,
	tactful,
	goBack
} from "../../Utils/Utils";

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		CustomBar: app.globalData.CustomBar, // 头部整体高度
		StatusBar: app.globalData.StatusBar, // 状态栏高度
		windowHeight: app.globalData.windowHeight, // 屏幕整体高度
		textAreaValue: "", // 用来保存用户提交的表单域里的内容
		textPlaceholder: "请输入交易地点或方式",
		BombBox2Show:false,					// 提示状态是否显示
		BombBox2text:"错误",				// 提示状态文字
		maxlength: 50,
		count: 0,
	},

	goBack: function (param = null) { // 返回键
		goBack()
	},

	optionBombBox2Show: function (param) {
		let that = this
		let temp = that.data.BombBox2Show
		if (temp) {
			return
		}
		that.setData({
			BombBox2Show: true,
			BombBox2text: param
		})
		setTimeout(() => {
			that.setData({
				BombBox2Show: false,
			})
		}, 1500)
	},

	editText: function (e) { // 获取表单域内容
		let maxlength = this.data.maxlength
		let temp = 0
		temp = e.detail.value.length > maxlength ? maxlength : e.detail.value.length
		let that = this
		that.setData({
			textAreaValue: e.detail.value,
			count: temp
		})
	},

	ensure: function () { // 确认按钮, 并返回数据
		let that = this
		let textAreaValue = that.data.textAreaValue
		let master = that.data.master
		let name = that.data.name
		if (textAreaValue) {
			
			if(textAreaValue.length>that.data.maxlength){
				that.optionBombBox2Show('超出字数限制')
				return
			}
			let tempText = tactful(textAreaValue)
			
			if (master == "beUsedToSell") {
				that.goBeUnusedToSell(tempText)
			}else if(master=="loseAndReceive"){
				that.goBeUnusedToSell(tempText)
			}else {
				if(name=="getUsername") {
					let username = tempText.replace(/\n/g, "").trim()
					if(username.length>10){
						that.optionBombBox2Show('用户名不合法')
						return false
					}
					db.collection("users").doc(app.globalData._id).update({
						data: {
							nickName: username,
						}
					}).then(res=>{
						// 改变上一页的内容
						that.goUserInfo(username, name)
						// 改变全局变量
						app.globalData.name = username;
						// 改变缓存
						bynameUpdate("userinfo.nickName", username)
					}).catch(err=>{
						console.log(err)
					})
				}else if(name=="getwxId") {
					let wxId = tempText.replace(/ |\n/g, "")
					db.collection("users").doc(app.globalData._id).update({
						data: {
							wxId: wxId,
						}
					}).then(res=>{
						// 改变上一页的内容
						that.goUserInfo(wxId, name)
						// 改变全局变量
						app.globalData.wxId = wxId;
						// 改变缓存
						bynameUpdate("userinfo.wxId", wxId)
					}).catch(err=>{
						console.log(err)
					})
				}else if(name=="getSign") {
					let sign = tempText.replace(/\n/g, "").trim()
					db.collection("users").doc(app.globalData._id).update({
						data: {
							sign: sign,
						}
					}).then(res=>{
						// 改变上一页的内容
						that.goUserInfo(sign, name)
						// 改变全局变量
						app.globalData.sign = sign;
						// 改变缓存
						bynameUpdate("userinfo.sign", sign)
					}).catch(err=>{
						console.log(err)
					})
				}else {
					that.goBack()
				}
			}

		} else {
			that.goBack()
		}
	},

	goBeUnusedToSell: function (detailInfo) {
		let eventChannel = this.getOpenerEventChannel();
		wx.navigateBack({
			delta: 1,
			success: function (res) {
				eventChannel.emit("getSite", {
					detailInfo
				})
			}
		})
	},

	goUserInfo: function (detailInfo, name) {
		let eventChannel = this.getOpenerEventChannel();
		wx.navigateBack({
			delta: 1,
			success: function (res) {
				eventChannel.emit(name, {
					detailInfo
				})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let that = this
		let event = this.getOpenerEventChannel()
		event.on('getData', function (data) {
			let {
				name
			} = data
			let {
				master
			} = data
			let {
				maxlength
			} = data
			let {
				textPlaceholder
			} = data
			that.setData({
				name,
				master,
				maxlength,
				textPlaceholder
			})
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})